﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using HYFamilyLibDataManager;

namespace HYRElectric.Dimension
{
	public partial class DlgWireDim : System.Windows.Forms.Form
	{
		public DbFamily SelectDbFamily
		{
			get
			{
				return this.selectDbFamily;
			}
		}

		public DlgWireDim(UIApplication uiApp)
		{
			this.InitializeComponent();
			this.m_application = uiApp;
			this.m_document = null;
			this.sysDB = null;
			this.selectDbFamily = null;
		}

		private void DlgWireLeadDim_Load(object sender, EventArgs e)
		{
			this.xmlHelper.LoadXml(this.path, "Root");
			//base.Icon = Resource.YArchitechIcon;
			this.m_document = this.m_application.ActiveUIDocument.Document;
			this.cbxSymbol.Items.Clear();
			try
			{
				this.sysDB = FamilyLibDatabase.GetSqliteDB(SubjectType.System, this.m_application.Application.VersionNumber);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			foreach (DbFamily item in this.sysDB.CategoryTable["电气标记"]["电线标记"]["导线标记"].GetAllFamilies(false, CheckType.All))
			{
				this.cbxSymbol.Items.Add(item);
			}
			if (this.cbxSymbol.Items.Count > 0)
			{
				this.cbxSymbol.SelectedIndex = 0;
			}
			this.LoadDlgData();
			this.selectDbFamily = (DbFamily)this.cbxSymbol.SelectedItem;
			this.pbView.Image = AssistFunc.ResizeImage((Bitmap)this.selectDbFamily.Image, 241, 80);
		}

		private void cbxSymbol_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.selectDbFamily = (DbFamily)this.cbxSymbol.SelectedItem;
			this.pbView.Image = AssistFunc.ResizeImage((Bitmap)this.selectDbFamily.Image, 241, 80);
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireDim"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireDim", "Params"));
			this.xmlHelper.SetAttribute("LeadDim", this.cbxLeadDim.Checked ? "True" : "False");
			this.xmlHelper.SetAttribute("DimSymbol", this.cbxSymbol.SelectedIndex.ToString());
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYWireDim", "Params"));
			string attribute = this.xmlHelper.GetAttribute("LeadDim");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "True")
				{
					this.cbxLeadDim.Checked = true;
				}
				else
				{
					this.cbxLeadDim.Checked = false;
				}
			}
			string attribute2 = this.xmlHelper.GetAttribute("DimSymbol");
			if (!string.IsNullOrEmpty(attribute2))
			{
				int num = Convert.ToInt32(attribute2);
				if (this.cbxSymbol.Items.Count >= num + 1)
				{
					this.cbxSymbol.SelectedIndex = num;
				}
			}
		}

		private void DlgWireDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "WireDim.xml");

		private const string Root = "Root";

		private const string Node = "HYWireDim";

		private const string ChildNode = "Params";

		private const string LeadDim = "LeadDim";

		private const string DimSymbol = "DimSymbol";

		private UIApplication m_application;

		private Document m_document;

		private FamilyLibDatabase sysDB;

		private DbFamily selectDbFamily;
	}
}
